package myapp.framelibrary.skin;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;

import myapp.framelibrary.skin.attrs.SkinAttr;
import myapp.framelibrary.skin.attrs.SkinType;
import myapp.framelibrary.skin.attrs.SkinView;

/**
 * Created by 超 on 2017/7/4.
 */

public class SkinAttrSupport {
    public static List<SkinAttr> getSkinAttrs(Context context, AttributeSet attrs) {

        List<SkinAttr> skinAttrs = new ArrayList<>();

        int attributeCount = attrs.getAttributeCount();

        for (int i = 0; i < attributeCount; i++) {

            String attrName = attrs.getAttributeName(i);

            String attrValue = attrs.getAttributeValue(i);

            SkinType skinType = getSkinType(attrName);

            if (skinType!=null){
                String resName = getResName(context,attrValue);
                if (TextUtils.isEmpty(resName)){
                    continue;
                }
                SkinAttr skinAttr = new SkinAttr(resName ,skinType);

                skinAttrs.add(skinAttr);
            }
        }
        return null;
    }
    //获取资源名称
    private static String getResName(Context context, String attrValue) {
        if (attrValue.startsWith("@")){
            attrValue = attrValue.substring(1);
            int resId = Integer.parseInt(attrValue);
            return context.getResources().getResourceEntryName(resId);
        }
        return null;
    }

    private static SkinType getSkinType(String attrName) {
        SkinType[] skinTypes = SkinType.values();
        for (SkinType skinType : skinTypes) {
            if (skinType.getResName().equals(attrName)){
                return skinType;
            }
        }
        return null;
    }
}
